package com.laicunba.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.laicunba.util.DateUtil;
import com.laicunba.util.HttpUtil;
import com.laicunba.util.MD5Util;
import com.laicunba.util.StringUtil;

@Service
public class MobileMessageService5iliulangImpl extends MobileMessageServiceYFImpl {
	private static final Logger LOG = Logger.getLogger(MobileMessageService5iliulangImpl.class);
	@Value("${mobile.msg.5iliulang.url}")
	private String liulangUrl;
	@Value("${mobile.msg.5iliulang.appkey}")
	private String liulangAppkey;
	@Value("${mobile.msg.5iliulang.appsecret}")
	private String liulangAppSecret;

	@Override
	public void sendMsgAsync(String mobile, String msg) {
		if (mock) {
			LOG.warn(String.format("5iliulang 短信未发送警告(由于 mock被设置为true)  手机号码:%s ,短信内容: %s", mobile, msg));
			return;
		}
		msg = "【来存吧】" + msg;
		String batchNum = Long.toString(System.currentTimeMillis()) + StringUtil.random(5);
		String timeStamp = DateUtil.formatDateTime(new Date());
		StringBuilder sb = new StringBuilder();
		sb.append("app_key").append("=").append(liulangAppkey).append("&");
		sb.append("batch_num").append("=").append(batchNum).append("&");
		sb.append("content").append("=").append(msg).append("&");
		sb.append("dest_id").append("=").append(mobile).append("&");
		sb.append("mission_num").append("=").append("1").append("&");
		sb.append("nonce_str").append("=").append("laicunba").append("&");
		sb.append("sms_type").append("=").append("verify_code").append("&");
		sb.append("time_stamp").append("=").append(timeStamp).append("&");
		sb.append("app_secret").append("=").append(liulangAppSecret);
		String sign = MD5Util.md5Hex(sb.toString());
		StringBuilder parameter = new StringBuilder();
		parameter.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml><head>");
		parameter.append("<app_key>").append(liulangAppkey).append("</app_key>");
		parameter.append("<time_stamp>").append(timeStamp).append("</time_stamp>");
		parameter.append("<nonce_str>").append("laicunba").append("</nonce_str>");
		parameter.append("<sign>").append(sign).append("</sign>");
		parameter.append("</head><body><dests><dest>");
		parameter.append("<mission_num>").append("1").append("</mission_num>");
		parameter.append("<dest_id>").append(mobile).append("</dest_id>");
		parameter.append("</dest></dests>");
		parameter.append("<batch_num>").append(batchNum).append("</batch_num>");
		parameter.append("<sms_type>").append("verify_code").append("</sms_type>");
		parameter.append("<content>").append(msg).append("</content>");
		parameter.append("</body></xml>");
		String result = HttpUtil.doPost(liulangUrl, parameter.toString());
		if (StringUtil.isNotEmpty(result)) {
			LOG.info(String.format("5iliulang 发送短信完毕  手机号[%s],内容[%s],发送结果[%s]", mobile, msg, result.replaceAll("\n", "")));
		} else {
			LOG.info(String.format("5iliulang 发送短信完毕  手机号[%s],内容[%s],发送失败,有可能接口不通", mobile, msg));
		}
	}
}
